import 'dart:math' as math;
import 'package:flutter/material.dart';
//
//转圈的动画组件
//
class RotationAnimation extends StatefulWidget {

  @override
  _RotationAnimationState createState() => _RotationAnimationState();

  final double width;
  final double height;
  final Widget widget;

  const RotationAnimation({Key? key, required this.width, required this.height, required this.widget}) : super(key: key);
}

class _RotationAnimationState extends State<RotationAnimation>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 2),
    )..repeat();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: widget.width,
      height: widget.height,
      child: AnimatedBuilder(
        animation: _controller,
        builder: (context, child) {
          final double angle = _controller.value * math.pi * 2;
          final double radius = widget.width/2;

          final centerX = widget.width / 2;
          final centerY = widget.height / 2;

          final x = centerX + math.cos(angle) * radius;
          final y = centerY + math.sin(angle) * radius;

          return Transform.translate(
            offset: Offset(x, y),
            child: widget.widget,
          );
        },
      ),
    );
  }
}